import 'package:flutter/material.dart';

class TapToTopModel with ChangeNotifier {
  final ScrollController? _scrollController;

  double? _height;

  bool? _showTopBtn = false;

  ScrollController? get scrollController => _scrollController;

  bool get showTopBtn => _showTopBtn!;

  TapToTopModel(this._scrollController, {height = 200});

  init() {
    _scrollController?.addListener(() {
      if (_scrollController!.offset > _height! && !_showTopBtn!) {
        _showTopBtn = true;
        notifyListeners();
      } else if (_scrollController!.offset < _height! && _showTopBtn!) {
        _showTopBtn = false;
        notifyListeners();
      }
    });
  }

  scrollToTop() {
    _scrollController?.animateTo(0, duration: const Duration(milliseconds: 300), curve: Curves.easeOutCubic);
  }
}
